<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <SCRIPT type="text/javascript">
        // 数据字典
        function saveData(){
           var name = $("#name_zh").val();
           var name_en = $("#name_en").val();
           
           var errorMsg = '';
           if(name == ''){
             errorMsg += "中文描述,";
           }
           
            if(name_en == ''){
             $("#name_en").val(name);
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveDataDicationary.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else if(data == 2){
                 alert("更新成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           });
           
        }
        
        
         //保存委托
        function saveItem(){
           var errorMsg = "";
           var name_zh = $("#name_zh").val();
           var name_en = $("#name_en").val();
           var type = $("#itemtype").val();
           
           if(name_zh == ''){
             errorMsg += "中文描述,";
           }
           
           if(name_en == ''){
             errorMsg += "中文描述,";
           }
           
           if(type == ''){
             errorMsg += "项目类型,";
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveCtsInfo.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           }); 
        }
        
        function modifyCtsStatus(statType , id){
           var status = $("input[name='status']:checked").val();
           if(statType == 2){ // company
              status = $("input[id='statusval']").val();
           }
           
           var url = "cts/modifyCtsStatus?statType="+statType;
           url += "&id="+id;
           url += "&status="+status;
           
           $.post(url , "" , function(data){
               if(data == 1){
                 alert("操作成功");
                 window.location.reload();
               }else{
                 alert("操作失败");
               }
           });
        }
        
        function companyChangeState(type , val){
            if(type == 1){
               $("#statusval").val(val);
            }else if(type == 2 ){
	           val = $("input[name='status1n']:checked").val();
               $("#statusval").val(val);
            }else if(type == 3 ){
               $("#statusval").val(val);
               $("#status11").attr("checked" , true);
            }
        }
        
        function saveCompany(){
           var companyAdmin = $("#userName").val();
           var companyName = $("#companyName").val();
           var companyPrefix = $("#companyPrefix").val();
           
           var errorMsg = "";
           if(companyAdmin.length <=0){
             errorMsg += "公司管理员不能为空,";
           }
           
           if(companyName.length <=0){
             errorMsg += "公司名称不能为空,";
           }
           
           if(companyPrefix.length <=0){
             errorMsg += "公司前缀不能为空,";
           }
           
          
          var reg = /[a-zA-Z]+$/
          if(!reg.test(companyPrefix) || companyPrefix.length > 3){
             errorMsg +="公司前缀限3个英文字符";
          }
          
          if(errorMsg.length > 0){
              alert(errorMsg);
              return;
           }
           
           companyPrefix = companyPrefix.substring(0,3);
           companyPrefix = companyPrefix.toUpperCase();
           $("#companyPrefix").val(companyPrefix);
           
           var param = $("#comapnyForm").serialize();
           $.post("cts/saveCompany.action" , param , function(data){
              if(data == 1){
                var companyid = '<s:property value="company.id"/>';
                if(companyid!=''){
                  alert("公司更新成功");
                  window.location.reload();
                }else{
                  alert("公司注册成功");
                  window.location.reload();
                }
                
                window.location.reload();
              }else if(data == -1){
                 alert("公司前缀已存在");
              }else if(data == -2){
                 alert("公司名字已存在");
              }else if(data == -3){
                 alert(companyAdmin + "已是公司管理员");
              }
           });
        }
        
        $(function(){
           var ctstype = '<s:property value="#request.ctstype"/>';
           if(ctstype == 'item'){
             var itemType = '<s:property value="item.type"/>';
             $("#itemtype").find("option[id="+itemType+"]").attr("selected" , "selected");
           }
           
           if(ctstype == 'company'){
             var companyId = '<s:property value="company.id"/>';
             if(companyId!=''){
               $("#0_companyId").val(companyId);
             }
           }
        });
        
        
     </SCRIPT>
     
     <STYLE type="text/css">
       .txtright{
         text-align: right;
       }
     </STYLE>
  </head>
  
  <body>
     <s:if test="#request.ctstype == 'data'">
         <form action="saveDataDicationary" id="from">
	         <input type="hidden" id="type" name="dataDictionary.type" value="<s:property value="#request.type"/>"/> 
	         <input type="hidden" id="typeDetail" name="dataDictionary.typeDetail" value="<s:property value="#request.typeDetail"/>"/> 
	         <s:if test="dataDictionary!=null">
		         <input type="hidden" id="id" name="dataDictionary.id" value="<s:property value="#request.id"/>"/> 
	         </s:if>
	         
	         <table style="font-size:12px;" cellpadding="0" cellspacing="0" align="center">
	            <tr>
	               <td>名称：</td>
	               <td><input type="text" id="name_zh" name="dataDictionary.name_zh" value="<s:property value="dataDictionary.name_zh"/>"/></td>
	            </tr>
	            <tr style="display:none;">
	               <td>英文描述：</td>
	               <td><input type="text" id="name_en" name="dataDictionary.name_en" value="<s:property value="dataDictionary.name_en"/>"/></td>
	            </tr>
	              <td>是否启用：</td>
	              <td>
	                <input value="0" type="radio" id="status1" name="dataDictionary.status" <s:if test="dataDictionary==null || dataDictionary.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="dataDictionary.status" <s:if test="dataDictionary.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center" style="padding-top:10px;">
	                  <input type="button" id="enterSave" value="确定" onclick="saveData()"/>
	                  &nbsp;&nbsp;&nbsp;
	                  &nbsp;&nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	     </form>
     </s:if>
     <s:elseif test="#request.ctstype == 'item'">
         <form action="saveCompayInfo" id="from">
	         <input type="hidden" id="ctstype" name="ctstype" value="<s:property value="#request.ctstype"/>"/> 
	         <input type="hidden" id="id" name="item.id" value="<s:property value="#request.id"/>"/> 
	         <input type="hidden" id="idx" name="id" value="<s:property value="#request.id"/>"/> 
	         
	         <table style="font-size:12px;" cellpadding="0" cellspacing="0" align="center">
	            <tr>
	               <td>中文描述：</td>
	               <td><input type="text" id="name_zh" name="item.name_zh" value="<s:property value="item.name_zh"/>"></td>
	            </tr>
	            <tr>
	               <td>英文描述：</td>
	               <td><input type="text" id="name_en" name="item.name_en" value="<s:property value="item.name_en"/>"></td>
	            </tr>
	            <tr>
	              <td>项目类型：</td>
	              <td>
	                <select id="itemtype" name="item.type">
	                   <option id="0" value="">--请选择--</option>
		                   <option id="1" value="1">整箱海运</option>
			               <option id="2" value="2">拼箱海运</option>
			               <option id="3" value="3">空运</option>
			               <option id="4" value="4">快递</option>
			               <option id="5" value="5">卡车运输</option>
	                </select>
	              </td>
	            </tr>
	            <tr>
	              <td>状态：</td>
	              <td>
	                <input value="0" type="radio" id="status1" name="item.status" <s:if test="item==null || item.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="item.status" <s:if test="item.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	              <td></td>
	              <td id="feelist">
	                <s:iterator value="#request.feelist" status="sta">
	                   <input type="checkbox" id="fee_${#sta.index }" value="<s:property value="id"/>" <s:property value="option1==1 ? 'checked' :''"/> /><s:property value="name"/>&nbsp;
	                </s:iterator>
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center"  style="padding-top:10px;">
	                  <input type="button" id="enterSave" value="确定" onclick="saveItem()"/>
	                  &nbsp;&nbsp;&nbsp;
	                  &nbsp;&nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	       </form>
     </s:elseif>
     <s:elseif test="#request.ctstype == 'user'">
        <table style="font-size:12px;" align="center">
          <tr>
             <td rowspan="5" valign="top" style="widht:70px;">
                <s:if test="user.logoPath!=null && user.logoPath!=''">
		           <img id="imgHead"  src="${pageContext.request.contextPath}/upload/user/<s:property value="user.id"/>/<s:property value="user.logoPath"/>"  style="width:40px;height:40px;"/>
               </s:if>
               <s:else>
                  <img id="imgHead"  src="${pageContext.request.contextPath}/cts/images/original.jpg"  style="width:40px;height:40px;position: absolute;margin-left:200px;margin-top:5px;"/>
               </s:else>
             </td>
          </tr>
          <tr>
             <td class="txtright">用户名：</td>
             <td><s:property value="user.userName"/></td>
          </tr>
          <tr>
             <td class="txtright">创建日期：</td>
             <td><s:date name="user.createTime" format="yyyy-MM-dd"/></td>
          </tr>
          <tr>
             <td class="txtright">
                    状态：
             </td>
             <td>
                <s:if test="user.status==0">
	                <input type="radio" id="status1" name="status" value="0" checked=checked'/>正常
	                <input type="radio" id="status2" name="status" value="1"/>禁用
                </s:if>
                <s:else>
                   <input type="radio" id="status1" name="status" value="0"/>正常
	               <input type="radio" id="status2" name="status" value="1" checked=checked'/>禁用
                </s:else>
             </td>
          </tr>
          <tr>
             <td colspan="2" align="center" style="text-align: center;">
                 <input type="button" id="save" name="n_save" value="保存" onclick="modifyCtsStatus( 1, <s:property value="user.id"/>)"/>
             </td>
          </tr>
        </table>
     </s:elseif>
     <s:elseif test="#request.ctstype == 'company'">
       <form id="comapnyForm" name="comapnyForm">
	        <table cellpadding="0" cellspacing="0" style="font-size:12px;">
	           <tr>
	              <td colspan="2">
	                 <s:if test="company == null || company.logoPath == '' || company.logoPath==null">
		               <img onclick="showUpLoadPage(this)" id="imgHeadCpy" src="${pageContext.request.contextPath}/cts/images/original.jpg" style="width:40px;height:40px;margin-top:4px;cursor:pointer;">
		             </s:if>
		             <s:else>
		               <img onclick="showUpLoadPage(this)" id="imgHeadCpy"  src="${pageContext.request.contextPath}/upload/company/<s:property value="company.id"/>/<s:property value="company.logoPath"/>"  style="width:40px;height:40px;margin-top:4px;cursor:pointer;"/>
		             </s:else>
	              </td>
	           </tr>
	           <tr>
	             <td class="txtright">公司管理员：</td>
	             <td>
	                <input type="text" id="userName" name="company.user.userName" value="<s:property value="company.user.userName"/>" onkeyup="searchUser(this,event)"/>
	                <input type="hidden" id="0_userId" name="company.user.id" value="<s:property value="company.user.id"/>"/>
	                <input type="hidden" id="compantPath" name="company.logoPath" value="<s:property value="company.logoPath"/>"/>
	                <input type="hidden" id="isTimeUp" name="isTimeUp"/>
	                <input type="hidden" id="timeFileName" name="timeFileName"/>
	                <s:if test="company!=null">
		                <input type="hidden" id="0_companyId" name="company.id" value=""/>
	                </s:if>
	             </td>
	           </tr>
	           <tr>
	             <td class="txtright">公司名称：</td>
	             <td>
	               <input type="text" id="companyName" name="company.name" value="<s:property value="company.name"/>"/>
	             </td>
	           </tr>
	           <tr>
	             <td class="txtright">公司前缀：</td>
	             <td>
	               <input type="text" id="companyPrefix" name="company.prefix" value="<s:property value="company.prefix"/>"/>
	             </td>
	           </tr>
	           <s:if test="">
	             <tr>
	               <td class="txtright">创建日期：</td>
	               <td><s:date name="company.createTime" format="yyyy-MM-dd"/></td>
	             </tr>
	           </s:if>
	           <tr>
	             <td class="txtright">
	                    审核状态：
	             </td>
	             <td id="pStatus1">
	                <s:if test="company.status==0">
		                <input type="radio" id="status1" name="status1n" value="0" checked=checked' onclick="companyChangeState(3 , 0)"/>未审核
		                <input type="radio" id="status2" name="status1n" value="1" onclick="companyChangeState(3 , 1)"/>已审核
	                </s:if>
	                <s:elseif test="company.status==1">
		                    <input type="radio" id="status1" name="status1n" value="0"  onclick="companyChangeState(3 , 0)"/>未审核
			                <input type="radio" id="status2" name="status1n" value="1" checked=checked' onclick="companyChangeState(3 , 1)"/>已审核
		            </s:elseif>
		            <s:else>
		                 <input type="radio" id="status1" name="status1n" value="0"  checked=checked' onclick="companyChangeState(3 , 0)"/>未审核
			             <input type="radio" id="status2" name="status1n" value="1"  onclick="companyChangeState(3 , 1)"/>已审核
		            </s:else>
	             </td>
	          </tr>
	          <tr>
	             <td class="txtright">是否启用：</td>
	             <td>
	               <s:if test="company.status==0 || company.status==1">
		                <input type="radio" id="status11" name="status11" value="0" checked=checked' onclick="companyChangeState(2 , 0)"/>启用
		                <input type="radio" id="status22" name="status11" value="1" onclick="companyChangeState(1 , 2)"/>禁用
	                </s:if>
	                <s:else>
		                <input type="radio" id="status11" name="status11" value="0" onclick="companyChangeState(2 , 1)"/>启用
		                <input type="radio" id="status22" name="status11" value="1" checked=checked' onclick="companyChangeState(1 , 2)"/>禁用
		            </s:else>
		            <input type="hidden" id="statusval" name="company.status" value="<s:property value="company.status"/>"/>
	             </td>
	          </tr>
	          <tr>
	             <td colspan="2" align="center" style="text-align: center;">
	                 <input type="button" id="save" name="n_save" value="保存" onclick="saveCompany()"/>
	             </td>
	          </tr>
	        </table>
       </form>
     </s:elseif>
     
  </body>
</html>


<SCRIPT type="text/javascript">
   function searchUser(obj , e){
       if(bindNextPrev(obj , e)){
          return;
       }
       
       var name = $(obj).val();
       if(name.replace(/\s/g , '') == ""){
          $("#userId").val('');
          $("#userlist").hide();
          return;
       }
       $.post("cts/fuzzyUser.action?name="+name , "" , function(data){
             var data2 = eval("("+data+")");
             showUserList(data2 , obj);
       });
    }
         
    function showUserList(data , obj){
       if(data.length <= 0){
          $("#userId").val('');
          $("#userlist").hide();
          return;
       }
       var mObj = $("#comapnyForm").find("div[id='userlist']");
       if(mObj == null || $(mObj).html() == null ){
         var divObj = '<div id="userlist" class="userlist"><ul style="width:100%;"></ul></div>';
         $(obj).before(divObj);
       }
       
       var li="";
       for(var i=0;i<data.length;i++){
          li += '<li id='+data[i]["userId"]+'>'+data[i]["userName"]+'</li>';
       }
       
       $("#userlist").find("ul").html(li);
       $("#userlist").find("li").each(function(){
           $(this).click(function(){
               selectUser(this);
               $("#userlist").hide();
           }).mouseover(function(){
               $(this).css({"background-color":"#ececec"});
           }).mouseout(function(){
               $(this).css({"background-color":"#ffffff"});
           });
       });
       
       var top = $(obj).offset().top+10;
       var left = $(obj).offset().left;
       
       top = 25;
       left = 0;
       $("#userlist").css({
          "margin-top":top+"px",
          "margin-left":left+"px"
       }).show();
    }
         
    function selectUser(obj){
        var id = $(obj).attr("id");
        var name = $(obj).text();
        $("#0_userId").val(id);
        $("#userName").val(name);
    }
     
    function myOnblur(obj , hideId){
       var time = null;
       time = window.setTimeout(function(){
           window.clearTimeout(time);
           $("#"+hideId).hide();
       } , 500);
    }
</SCRIPT>

<script type="text/javascript">
var selectObject = null;
function mouseMoveCha(){
    var contentlist = $("#userlist");
    contentlist.each(function(i){
        $(this).mouseout(function(){
    		$(contentlist).find("li").css({"background-color":"#fff"});
            $(this).css({"background-color":"#ddd"});
        }).mouseover(function(){
        
        });
    });
	      
}

function bindNextPrev(obj , e){
    mouseMoveCha();
          
    var theEvent = window.event || e;
    if(typeof(theEvent) == 'undefined'){
      return true;
    }
       
    var keyFee = theEvent.keyCode || theEvent.which;
    var isGo = false;
	var contentlist = $("#userlist");
    
    if (keyFee == 38) {// prev
        if(selectObject == null){
           selectObject = $(contentlist).find("li:last");
        }else{
           selectObject = $(selectObject).prev();
           if($(selectObject).html() == null){
              selectObject = $(contentlist).find("li:last");
           }
        }
	    isGo = true;
	}
	
	if (keyFee == 40) {// next
	    if(selectObject == null){
           selectObject = $(contentlist).find("li:first");
        }else{
           selectObject = $(selectObject).next();
           if($(selectObject).html() == null){
              selectObject = $(contentlist).find("li:first");
           }
        }
	    isGo = true;
	}
	
	if(keyFee == 13){ // enter
	    if(selectObject!=null && $(selectObject).html() !=null){
	        selectUser(selectObject);
	    }
	    
	    $(contentlist).hide();
	    return true;
	}
	
	if(keyFee == 27){
	   $(contentlist).hide();
	    return true;
	}
	
	// change the background color
	if(isGo == true){
	   if($(selectObject).html() !=null){
	      $(contentlist).find("li").css({"background-color":"#fff"});
	      $(selectObject).css({"background-color":"#ddd"});
	   }
	   
	   return true;
	}
      }
      
</script>

<script type="text/javascript">
    // 个人近照
    function showUpLoadPage(obj){
	     if(!(document.cookie || navigator.cookieEnabled)){
	        alert("请开启cookie");
	        return;
	     }
	     
	     var url = "toImageTool?uptype=2&ishead=true&randomnumber="+randomnumber;
	     var companyId = '<s:property value="company.id"/>';
	     if(companyId!=''){
	       url += "&companyid="+companyId;
	     }
	     
	     var randomnumber = Math.floor(Math.random() * 100000);
	     $("#uploadImage").find("iframe[id='headIframeI']").remove();
	     $("#uploadImage").find("div[class='popdiv_content']").html('<iframe id="headIframeI" src="javascript:void(0)" style="width:317px;height:420px;margin:0px;padding:0px;" frameborder="0" scrolling="no"></iframe>'); 
		 $("#uploadImage").find("iframe[id='headIframeI']").attr("src" , url);  
	     $("#uploadImage").find("div[class='popdiv_handle']").text("公司LOGO");
	     openPopDiv( 'uploadImage' , '' ,  200); 
   }
   
  function imgReturnVal(isTimeFile , fileBookName , imgUrl , imgName){
     var randomnumber = Math.floor(Math.random() * 100000);
     imgUrl = '${pageContext.request.contextPath}/' + imgUrl+"?time="+randomnumber;
     $("#imgHeadCpy").attr("src" , imgUrl);
     $("#compantPath").val(imgName);
     $("#isTimeUp").val(isTimeFile);
     $("#timeFileName").val(fileBookName);
     
     $("#uploadImage").hide();
  }
</script>
   
<style type="text/css">
  .txtright{
    text-align: right;
  }
  .authDiv{
    height:14px;width:120px;float:left;
  }
  
  .userlist{
    width:200px;
    height:auto;
    border:1px solid gray;
    background-color:white;
    position: absolute;
  }
  
  .userlist ul li{
     width:100%;
     height:20px;
     line-height:20px;
     border-bottom:1px dotted gray;
     font-size:12px;
  }
  
  .background1{
    background-color:#ececec;
  }
  
  .background2{
    background-color:#ffffff;
  }
</style>
